/**
 * Copyright (C)  Gadglet .
 *
 * This file is part of Gadglet
 *
 * Gadglet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gadglet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Gadglet. If not, see <http://www.gnu.org/licenses/>.
 */

package com.gadglet.client.gwt;

import java.util.Map;

import com.gadglet.params.GadgetType;
import com.google.gwt.gadgets.client.impl.PreferencesUtil;

/**
 * GWT Client class represent constants for this Gadget. may add more Gadget
 * level params using "gadgetParams"
 * Developer must use it in the Gadget root class in the init() method
 * 
 */
public class GadgetConfig {

	public String debudCode = "basad"; // default debug code
	private static GadgetConfig gadgetConfig;
	private String gadgetName = null;
	protected final PreferencesUtil prefs = PreferencesUtil.nativeInitPrefs();
	private Map<String, String> gadgetParams = null;
	private GadgetType gadgetType = null;

	public GadgetType getGadgetType() {
		if (gadgetType != null)
			return gadgetType;
		else
			return GadgetType.OTHER;
	}

	/**
	 * @return the debug code
	 */
	public String getDebudCode() {
		return debudCode;
	}

	/**
	 * @param debudCode
	 *            - change the default debug code
	 */
	public void setDebudCode(String debudCode) {
		this.debudCode = debudCode;
	}

	/**
	 * @param gadgetType
	 *            - Gadget type: minglet/bizlet/other should be used in the
	 *            Gadget root class in the init() method example
	 *            setGadgetType(GadgetType.BIZLET);
	 * 
	 *            In general there is no need to set the name of the Gadget,
	 *            will be calculated from the url provided by the container
	 */
	public void setGadgetType(GadgetType gadgetType) {
		this.gadgetType = gadgetType;
	}

	@Deprecated
	public static GadgetConfig getGadgetConfig(String gadgetName) {
		if (gadgetConfig == null) {
			gadgetConfig = new GadgetConfig(gadgetName);
		}
		return gadgetConfig;
	}

	public static GadgetConfig getGadgetConfig() {
		if (gadgetConfig == null) {

			String gadgetName = GadgetUtils.getGadgetNameFromXmlUrl();
			gadgetConfig = new GadgetConfig(gadgetName);
		}
		return gadgetConfig;
	}

	public static GadgetConfig getGadgetConfig(Map<String, String> params) {
		if (gadgetConfig == null) {

			String gadgetName = GadgetUtils.getGadgetNameFromXmlUrl();
			gadgetConfig = new GadgetConfig(params, gadgetName);
		}
		return gadgetConfig;
	}

	private GadgetConfig(Map<String, String> params, String gadgetName) {
		this.gadgetParams = params;
		this.gadgetName = gadgetName;
	}

	private GadgetConfig(String gadgetName) {
		this.gadgetName = gadgetName;
	}

	public String getGadgetParam(String paramName) {
		return gadgetParams.get(paramName);
	}

	public void setGadgetParams(Map<String, String> gadgetParams) {
		this.gadgetParams = gadgetParams;
	}

	public String getGadgetName() {
		return this.gadgetName;
	}

	/**
	 * @return The debug code user enter to the Gadget "tCode" pref.
	 */

	public String getDebugServerCode() {
		if (isDebugMode()) {
			String serverCode = null;
			try {
				serverCode = prefs.getString("tCode").substring(
						prefs.getString("tCode").indexOf("=") + 1);
			} catch (Exception e) {

			}
			return serverCode;
		} else
			return null;
	}

	/**
	 * @return true - when the user enter the debug code to the Gadget "tCode"
	 *         pref.
	 */
	public boolean isDebugMode() {
		boolean isDebugMode = false;

		try {
			if (prefs.getString("tCode") != null
					&& prefs.getString("tCode").startsWith(debudCode))
				isDebugMode = true;

		} catch (Exception e) {

		}

		return isDebugMode;
	}

}
